package main

import (
	"fmt"
	"os"
	"sanguo/cluster"
	"sanguo/node/node_team"
	"sanguo/node/node_team/com"
	"sanguo/node/node_team/match"
	_ "sanguo/node/node_team/onOroff"
	_ "sanguo/node/node_team/team"
	_ "sanguo/protocol/cs"
	"strconv"
	"strings"

	"github.com/go-ini/ini"
	"github.com/sniperHW/kendynet"
	"github.com/sniperHW/kendynet/golog"
)

func main() {

	if len(os.Args) < 2 {
		fmt.Printf("usage ./node_team config\n")
		return
	}

	outLogger := golog.NewOutputLogger("log", "node_team", 1024*1024*50)
	kendynet.InitLogger(outLogger, "node_team")
	cluster.InitLogger(outLogger, "node_team")

	node_team.InitLogger(outLogger, "node_team")

	cfg, err := ini.LooseLoad(os.Args[1])
	if err != nil {
		node_team.Errorf("load %s failed:%s\n", os.Args[1], err.Error())
		return
	}

	secCommon := cfg.Section("Common")

	if nil == secCommon {
		node_team.Errorf("missing sec[Common]\n")
		return
	}

	sec := cfg.Section(os.Args[2])

	if nil == sec {
		node_team.Errorf("missing sec[%s]\n", os.Args[2])
		return
	}

	centerAddr := secCommon.Key("centerAddr").Value()
	clusterAddr := sec.Key("clusterAddr").Value()

	t := strings.Split(clusterAddr, ":")

	ip := t[0]
	port, _ := strconv.Atoi(t[1])

	err = cluster.Start(strings.Split(centerAddr, ","), cluster.MakeService("team", ip, int32(port)))

	if !com.Init(cluster.MakeService("team", ip, int32(port))) {
		node_team.Errorln("Init failed")
		return
	}

	if !match.Init() {
		node_team.Errorln("Init Match failed")
		return
	}

	if nil != err {
		node_team.Errorf("node_team start failed:%s\n", err.Error())
		return
	}

	sigStop := make(chan bool)
	_, _ = <-sigStop

}
